﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace BLL
{
    /// <summary>
    /// IO方法
    /// </summary>
    public class IOHelper
    {
        /// <summary>
        /// 写TXT方法
        /// </summary>
        /// <param name="Path">路径</param>
        /// <param name="contents">内容</param>
        /// <param name="fileMode">读写模式</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static bool Writer_TXT(string Path, string[] contents,FileMode fileMode,Encoding encoding)
        {
            bool re = false;

            try
            {
                FileStream fs = new FileStream(Path, fileMode, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs, encoding);
                sw.Flush();
                foreach (string s in contents)
                {
                    sw.WriteLine(s);
                }
                sw.Close();
                sw.Dispose();
                fs.Close();
                fs.Dispose();
                re = true;
            }
            catch(Exception ex)
            {
                re = false;
            }
            return re;

        }

        /// <summary>
        /// 写入TXT
        /// </summary>
        /// <param name="Path">地址</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static List<string> Read_TXT(string Path, Encoding encoding)
        {
            List<string> rels=new List<string>();
            try
            {
                FileStream fs = new FileStream(Path, FileMode.Open);
                StreamReader sr = new StreamReader(fs, encoding);
                while (!sr.EndOfStream)
                {
                    rels.Add(sr.ReadLine());
                }
            }
            catch (Exception ex)
            {
                if (ex.ToString().Split(':')[0] == "System.IO.FileNotFoundException")
                {
                    rels.Add("System.IO.FileNotFoundException");
                }
                else
                {
                    rels.Add("Exception");
                }
                
            }
            return rels;
        }

        /// <summary>
        /// 计算文件的MD5校验
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetMD5HashFromFile(string fileName)
        {
            try
            {
                FileStream file = new FileStream(fileName, FileMode.Open);
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(file);
                file.Close();

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
            }
        }


    }
}
